package com.jd.myaaaccount;

import com.jd.myaaaccount.entity.User;
import com.jd.myaaaccount.model.Common;
import com.jd.myaaaccount.model.UserModel;
import com.jd.myaaaccount.util.AlertDialogHelper;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.MD5Helper;
import com.jd.myaaaccount.util.OperateCode;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends BaseActivity {

	private EditText etName;
	private EditText etPassword;
	private Button btnLogin;
	private TextView tvreglink;
	private TextView tvfindlink;

	private UserModel um;
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		um = new UserModel(this);
		iniControl();

		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				pd.cancel();
				switch (msg.what) {
				case OperateCode.LOGIN:// 登录结果
					pd.cancel();
					if (msg.arg1 == ErrorCode.SUCCESS) {// 登录成功
						Intent intent = new Intent();
						intent.setClass(LoginActivity.this, MainActivity.class);
						startActivity(intent);
						finish();
					} else {
						Common.showErrorInfo(LoginActivity.this, msg.arg1, "登录失败",0);
					}
					break;
				default:
					break;
				}
			};
		};

		// 登录
		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				login();
			}
		});
		// 找回密码
		tvfindlink.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});
		// 注册
		tvreglink.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(LoginActivity.this, RegistActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	private void iniControl() {
		etName = (EditText) findViewById(R.id.etName);
		etPassword = (EditText) findViewById(R.id.etPassword);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		tvreglink = (TextView) findViewById(R.id.tvRegist);
		tvfindlink = (TextView) findViewById(R.id.tvFindPassword);

	}

	private void login() {

		String email = etName.getText().toString();
		String password = etPassword.getText().toString();

		if (email.equals("")) {
			Toast.makeText(LoginActivity.this, "请填写邮箱", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (password.equals("")) {
			Toast.makeText(LoginActivity.this, "请填写密码", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		password = MD5Helper.md5(password);
		final User user = new User();
		user.setEmail(email);
		user.setPassword(password);
		pd = AlertDialogHelper.showWaitProgressDialog(LoginActivity.this,
				"登录中…");
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				um.login(user, handler);
			}
		}).start();

	}
}
